var express = require("express");
var router = express.Router();
const { messageModel } = require("../models/model");

// 获取用户消息列表
router.get("/userId", async (req, res) => {
  const { userId, friendId } = req.query;
  console.log(userId, friendId);

  try {
    const message = await messageModel
      .find({
        $or: [
          { userId, friendId },
          { userId: friendId, friendId: userId },
        ],
      })
      .populate("userId friendId"); //查找该用户的信息
    console.log(message, "message--------------------------------");
    res.send({
      code: 200,
      result: message,
    });
  } catch (error) {
    console.error("Error fetching messages: ", error.message || error);
    res.status(500).json({ message: "获取消息列表失败", error: error.message });
  }
});

module.exports = router;
